import requests

def get_google_ip():
    url = "https://www.gstatic.com/ipranges/goog.json"
    response = requests.get(url)
    data = response.json()
    ip_list = []
    for prefix in data["prefixes"]:
        if "ipv4Prefix" in prefix:
            ip_list.append(prefix["ipv4Prefix"])
    return ip_list

def get_googleroute4txt():
    ip_list = get_google_ip()
    with open("google_address_route.txt", "w") as file:
        for ip in ip_list:
            file.write(ip + "\n")
    print("Google地址列表已生成：google_address_route.txt")

def get_google_route4ros(gateway):
    ip_list = get_google_ip()
    script = f"/ip route \n"
    for ip in ip_list:
        script += f":do {{add dst-address={ip} gateway={gateway} }} on-error={{}}\n"
    with open("google_address_route.rsc", "w") as file:
        file.write(script)
    print("RouterOS脚本已生成：google_address_route.rsc")

get_googleroute4txt()

